package com.nl.controller.xuyi.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import com.alibaba.fastjson.JSONObject;
import com.nl.controller.common.model.BaseResult;
import com.nl.controller.web.bean.BdResult;
import com.nl.controller.web.bean.XuYiStation;
import com.nl.controller.web.mapper.SmsAreaMapper;
import com.nl.controller.web.service.SmsInterfacesService;
import com.nl.controller.web.util.FileUtil;
import com.nl.controller.web.util.HttpClientUtil;
import com.nl.controller.xuyi.mapper.XuYiPathMapper;
import com.nl.controller.xuyi.mapper.XuYiSubStationMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class XuYiSubStationService {
	@Autowired
	SmsInterfacesService smsInterfacesService;
	@Autowired
	XuYiSubStationMapper xuYiSubStationMapper;
	@Autowired
	XuYiPathMapper xuYiPathMapper;
	
	@Autowired
	SmsAreaMapper smsAreaMapper;

	@Autowired
	XuYiPointService xuYiPointService;
	
	
	public int subStationAdd(String pathName,String regionName,String lng,String lat,String remark,String operatorTel){
		int result = 0;
		try {
			XuYiStation subStationInfo=new XuYiStation();

			String lngLat = SmsInterfacesService.GPSToBd(lng, lat);
			String bdLng = "";
			String bdLat = "";
			bdLng = lngLat.split(",")[0];
			bdLat = lngLat.split(",")[1];
			subStationInfo.setName(regionName);
			subStationInfo.setPathName(pathName);
			subStationInfo.setLng(lng);
			subStationInfo.setLat(lat);
			subStationInfo.setBdLng(bdLng);
			subStationInfo.setBdLat(bdLat);
			subStationInfo.setOperatorTel(operatorTel);
			subStationInfo.setRemark(remark);
			subStationInfo.setIsValid("1");
			result = xuYiSubStationMapper.subStationAdd(subStationInfo);	
			System.out.println(regionName+","+lng+","+lat+","+bdLng+","+bdLat+","+remark+","+operatorTel);
			
		} catch (Exception e) {
			e.printStackTrace();
			log.info("坐标转化异常："+ e.toString());
			result = -1;
		}
		return result;
	}
	
	
	public int subStationModify(String regionName,String remark,String subSeq,String operatorTel){
		int result = 0;
		try {
			XuYiStation subStationInfo=new XuYiStation();
			subStationInfo.setName(regionName);
			subStationInfo.setRemark(remark);
			subStationInfo.setSubSeq(subSeq);
			subStationInfo.setOperatorTel(operatorTel);
			result = xuYiSubStationMapper.subStationModify(subStationInfo);	
		} catch (Exception e) {
			e.printStackTrace();
			result = -1;
		}
		return result;
	}
	
	
	public int deleteSubStation(String pathName,String regionName) {
		int result =0;
		try {
			 result = xuYiSubStationMapper.deleteSubStation(pathName,regionName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public int saveSubFileData(String path,String operatorTel){
		int dataCount=0;
		List list;
		//uYiStation subStationInfo=new XuYiStation();
		String pointName ="";
		String gpsLng = "";
		String gpsLat = "";
		String gpsLngLats = "";
		Map<String, String> nameMap = new HashMap<>();

		String[] arr;
		String x = "";
		String y = "";
		list = FileUtil.readCSV(path);
		for(int i=0;i<list.size();i++){
			try {
				arr=list.get(i).toString().split(",");
				pointName = arr[0];

				gpsLng = arr[1];
				gpsLat = arr[2];

				if(nameMap.containsKey(pointName)){
					gpsLngLats = nameMap.get(pointName)+"|"+gpsLng+","+gpsLat;
					nameMap.put(pointName,gpsLngLats);
				}else{
					nameMap.put(pointName,gpsLng+","+gpsLat);
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info("数据插入异常");
				return -1;
			}

		}
		//遍历入表
		for ( Map.Entry< String , String > entry : nameMap.entrySet ( ) ) {
			int result = xuYiPointService.pointInfoAdd(entry.getKey(),entry.getValue(),"",operatorTel);
			if(result > 0){
				dataCount++;
			}
		}

		//nt result= SmsMgrTemplateInfoMapper.saveFileData()
		return dataCount;
	}
	
	public int regionRelateAdd(String pathName, String regionList,String operatorTel) {
		String[] regions = regionList.split(",");
		String regionName = "";
		int result = 0;
		xuYiPathMapper.deletePath(pathName);
		for(int i = 0 ; i<regions.length;i++){
			regionName = regions[i];
			List<XuYiStation> subStationList = smsAreaMapper.findXuYiSubStations("",regionName,"");
			for(XuYiStation subStationInfo : subStationList){
					subStationInfo.setPathName(pathName);
					subStationInfo.setOperatorTel(operatorTel);
					subStationInfo.setIsValid("1");
					result = xuYiSubStationMapper.subStationAdd(subStationInfo);
			}
		}
		return result;
	}


	public String findlatlngListByRegionName(String regionName) {
		String latlngString = "";
		String lat = "";
		String lng = "";
		List<XuYiStation> xuYiStationList = xuYiSubStationMapper.findlatlngListByRegionName(regionName);
		if(!CollectionUtils.isEmpty(xuYiStationList)){
			for(int i=0;i<xuYiStationList.size();i++){
				lat = xuYiStationList.get(i).getLat();
				lng = xuYiStationList.get(i).getLng();
				if(i==0){
					latlngString = lng+","+lat;
				}else{
					latlngString  = latlngString+ "|"+lng+","+lat;
				}
				
			}
		}
		log.info("---"+latlngString);
		return latlngString;
	}

}
